﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using U3DSceneEditor;

public class DrawComponent : MonoBehaviour
{
    public bool _bGiszmosEnable = false;

    public void EnableGizmos(bool flag)
    {
        _bGiszmosEnable = flag;
    }

    private float _Width = 0;
    private float _Height = 0;
    public List<Vector3> _listRectangUtil = new List<Vector3>(5);

    private float _H_Diagonal = 0;
    private float _V_Diagonal = 0;
    public List<Vector3> _listRhombus = new List<Vector3>(5);
    public Dictionary<Vector3, Vector3> _listRhombusSpilt = new Dictionary<Vector3, Vector3>();

    private Rect rect = new Rect();

    public void ReRhombus(float h_d, float v_d)
    {
        _H_Diagonal = h_d;
        _V_Diagonal = v_d;

        RefreshRhombus();

    }

    public void RefreshRhombus()
    {
        //  画外边菱
        _listRhombus.Clear();
        _listRhombus.Add(transform.position + new Vector3(0, _V_Diagonal / 2, 0));
        _listRhombus.Add(transform.position + new Vector3(_H_Diagonal / 2, _V_Diagonal, 0));
        _listRhombus.Add(transform.position + new Vector3(_H_Diagonal, _V_Diagonal / 2, 0));
        _listRhombus.Add(transform.position + new Vector3(_H_Diagonal / 2, 0, 0));
        _listRhombus.Add(transform.position + new Vector3(0, _V_Diagonal / 2, 0));

        //  小菱形
        float n = (float)(_H_Diagonal / ZoneCfg.RhombusMin);
        if (n > 1)
        {
            _listRhombusSpilt.Clear();
            SplitRhombus(n,
                            transform.position + new Vector3(0, _V_Diagonal / 2, 0),
                            transform.position + new Vector3(_H_Diagonal / 2, _V_Diagonal, 0),
                            transform.position + new Vector3(_H_Diagonal, _V_Diagonal / 2, 0),
                            transform.position + new Vector3(_H_Diagonal / 2, 0, 0));
        }

        Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
        rect.x = screenPos.x + 25;
        rect.y = Screen.height - screenPos.y - 25;
        rect.width = 256;
        rect.height = 50;

    }

    /// <summary>
    /// 画边框
    /// </summary>
    /// <param name="center"></param>
    /// <param name="size"></param>
    public void ReBorder(float width, float heigh)
    {
        _Width = width;
        _Height = heigh;

        RefreshBorder();
    }

    public void RefreshBorder()
    {
        //  画外边框
        _listRectangUtil.Clear();
        _listRectangUtil.Add(transform.position);
        _listRectangUtil.Add(transform.position + new Vector3(0, _Height, 0));
        _listRectangUtil.Add(transform.position + new Vector3(_Width, _Height, 0));
        _listRectangUtil.Add(transform.position + new Vector3(_Width, 0, 0));
        _listRectangUtil.Add(transform.position);

        //  文字
        Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
        rect.x = screenPos.x;
        rect.y = Screen.height - screenPos.y - 10;
        rect.width = Screen.width;
        rect.height = 50;
    }

    /// <summary>
    /// 分割菱形成基础单元
    /// </summary>
    /// <param name="n"></param>
    /// <param name="left"></param>
    /// <param name="top"></param>
    /// <param name="right"></param>
    /// <param name="bottom"></param>
    void SplitRhombus(float n, Vector3 left, Vector3 top, Vector3 right, Vector3 bottom)
    {
        float dis = Vector3.Distance(left, top);
        float sec_dis = dis / n;

        Vector3 dir_up = (top - left).normalized;
        Vector3 dir_dn = (bottom - left).normalized;

        for (int i = 1; i < n; ++i)
        {
            Vector3 begin_up = left + dir_up * sec_dis * i;
            Vector3 end_up = bottom + dir_up * sec_dis * i;

            _listRhombusSpilt.Add(begin_up, end_up);

            Vector3 begin_dn = left + dir_dn * sec_dis * i;
            Vector3 end_dn = top + dir_dn * sec_dis * i;

            _listRhombusSpilt.Add(begin_dn, end_dn);

        }
    }

    void Update()
    {
        if (!_bGiszmosEnable)
            return;

        ShowGizmoz();
    }

    void ShowGizmoz()
    {
        for (int i = 0; i < _listRectangUtil.Count - 1; ++i)
        {
            GLGizmos.DrawLine(_listRectangUtil[i], _listRectangUtil[i+1], Color.black);
        }
        for (int i = 0; i < _listRhombus.Count - 1; ++i)
        {
            GLGizmos.DrawLine(_listRhombus[i], _listRhombus[i + 1], Color.black);
        }
        foreach (KeyValuePair<Vector3, Vector3> line in _listRhombusSpilt)
        {
            GLGizmos.DrawLine(line.Key, line.Value, Color.black);
        }

    }

#if DEBUG
    private void OnGUI()
    {
        if (!_bGiszmosEnable)
            return;

        GUI.Label(rect, " [" + transform.name + "] " + _content);
    }
#endif

    string _content = string.Empty;

    public void RefreshDescription(string content)
    {
        _content = content;
    }
}
